package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.bean.Dish;
import com.itheima.bean.DishFlavor;
import com.itheima.common.ParamPage;
import com.itheima.dao.DishDao;
import com.itheima.dto.DishDto;
import com.itheima.exception.CustomException;
import com.itheima.service.CategoryService;
import com.itheima.service.DishFlavorService;
import com.itheima.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class DishServiceImpl implements DishService {
    @Autowired
    private DishDao dishDao;
    @Autowired
    private DishFlavorService dfs;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private CategoryService categoryService;
    @Override
    public List<Dish> findByCid(Long id,Integer status) {
        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Dish::getCategoryId,id);
        lqw.eq(status!=null,Dish::getStatus,status);
        return dishDao.selectList(lqw);
    }

    @Override
    public int add(DishDto dishDto) {
        if (dishDao.insert(dishDto)<1){
            return -1;
        }
        if(dishDto.getFlavors()!=null&&dishDto.getFlavors().size()>0){
            int a=0;
            for (DishFlavor flavor : dishDto.getFlavors()) {
                flavor.setDishId(dishDto.getId());
                a+=dfs.add(flavor);
            }
            if (a!=dishDto.getFlavors().size()){
                return 0;
            }
        }
        redisTemplate.delete(dishDto.getCategoryId()+"_"+dishDto.getStatus());
        return 1;
    }

    @Override
    public IPage<Dish> findpage(ParamPage paramPage) {
        IPage<Dish> page = new Page<>(paramPage.getPage(),paramPage.getPageSize());
        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
        lqw.like(paramPage.getName()!=null,Dish::getName,paramPage.getName());
        return dishDao.selectPage(page,lqw);
    }

    @Override
    public DishDto findByid(Long id) {
        Dish dish = dishDao.selectById(id);
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish,dishDto);
        dishDto.setFlavors(dfs.findByDid(dish.getId()));
        dishDto.setCategoryName(categoryService.findByCid(dish.getCategoryId()).getName());
        return dishDto;
    }

    @Override
    public int update(DishDto dishDto) {
        int i = dishDao.updateById(dishDto);
        List<DishFlavor> flavors = dishDto.getFlavors();
        int i1 = dfs.deleteByDid(dishDto.getId());
        int a=0;
        if (flavors.size()>0){
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(dishDto.getId());
                a+=dfs.add(flavor);
            }
        }
        redisTemplate.delete(dishDto.getCategoryId()+"_"+1);
        return i>0&&a==flavors.size()?1:0;
    }

    @Override
    public int delete(List<Long> ids) {
        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
        lqw.in(Dish::getId,ids);
        List<Dish>dishes = dishDao.selectList(lqw);
        lqw.eq(Dish::getStatus,1);
        if (dishDao.selectCount(lqw)>0){
            throw new CustomException("删除的菜品中有正在启售的菜品，禁止删除！");
        }
        int i = dishDao.deleteBatchIds(ids);
        if (ids.size()>0){
            for (Long id : ids) {
                dfs.deleteByDid(id);
            }
        }
        for (Dish dish : dishes) {
            redisTemplate.delete(dish.getCategoryId()+"_"+1);
        }
        return i==ids.size()?1:0;
    }

    @Override
    public int updatestatus(int status, List<Long> ids) {
        int a=0;
        if (ids!=null&&ids.size()>0){
            for (Long id : ids) {
                Dish dish = dishDao.selectById(id);
                dish.setStatus(status);
                redisTemplate.delete(dish.getCategoryId()+"_"+1);
                a+=dishDao.updateById(dish);
            }
        }
        return a==ids.size()?1:0;
    }

    @Override
    public List<Dish> findByCid1(String name) {
        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
//        lqw.eq(dish.getId()!=null,Dish::getId,dish.getId());
        lqw.like(Dish::getName,name);
        return dishDao.selectList(lqw);
    }
}
